package com.codedancer.campus.client.api.dynamic;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.dynamic.ro.DynamicCommentAddRo;
import com.codedancer.campus.client.api.dynamic.ro.DynamicCommentPageSearchRo;
import com.codedancer.campus.client.service.IDynamicCommentService;
import com.codedancer.campus.client.service.bo.dynamic.DynamicCommentAddBo;
import com.codedancer.campus.client.service.bo.dynamic.DynamicCommentPageSearchBo;
import com.codedancer.campus.client.service.vo.dynamic.DynamicCommentPageVo;
import com.codedancer.campus.common.anno.CommentNumber;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.enums.CommentNumberKeyEnum;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "动态评论api", description = "动态评论api", tags = "动态评论api")
@RequestMapping("/api/dynamic-comment")
public class DynamicCommentApi extends BaseApi {

    @Autowired
    private IDynamicCommentService dynamicCommentService;

    @PostMapping("/add/{dynamicUuid}")
    @ApiOperation("新增评论")
    @CommentNumber(description = "动态评论", key = CommentNumberKeyEnum.DYNAMIC_COMMENT)
    public ApiResponse<Void> add(
        @PathVariable(value = "dynamicUuid", required = true) @ApiParam(value = "动态uuid", required = true) String dynamicUuid,
        @RequestBody @Valid @ApiParam(value = "评论Ro", required = true)
        DynamicCommentAddRo dynamicCommentAddRo) {
        DynamicCommentAddBo dynamicCommentAddBo = BeanUtil
            .copyProperties(dynamicCommentAddRo, DynamicCommentAddBo.class);
        dynamicCommentAddBo.setCurrentUserUuid(currentLoginUuid());
        dynamicCommentAddBo.setDynamicUuid(dynamicUuid);
        dynamicCommentService.add(dynamicCommentAddBo);
        return success();
    }

    @GetMapping("/page/parent-comment/{dynamicUuid}")
    @ApiOperation("分页查询动态父级评论")
    public ApiResponse<IPage<DynamicCommentPageVo>> pageParentComment(
        @PathVariable(value = "dynamicUuid", required = true) @ApiParam(value = "动态uuid", required = true) String dynamicUuid,
        @ApiParam(value = "动态分页查询Ro", required = false) DynamicCommentPageSearchRo dynamicCommentPageSearchRo) {
        DynamicCommentPageSearchBo dynamicCommentPageSearchBo = BeanUtil
            .copyProperties(dynamicCommentPageSearchRo, DynamicCommentPageSearchBo.class);
        dynamicCommentPageSearchBo.setDynamicUuid(dynamicUuid);
        return success(dynamicCommentService.pageParentComment(dynamicCommentPageSearchBo));
    }

    @PostMapping("/reply/{dynamicCommentUuid}")
    @ApiOperation("动态回复")
    public ApiResponse<Void> reply(
        @PathVariable(value = "dynamicCommentUuid", required = true) @ApiParam(value = "评论uuid", required = true) String dynamicCommentUuid,
        @RequestBody @Valid @ApiParam(value = "动态评论添加Ro", required = true)
        DynamicCommentAddRo dynamicCommentAddRo) {
        DynamicCommentAddBo dynamicCommentAddBo = BeanUtil
            .copyProperties(dynamicCommentAddRo, DynamicCommentAddBo.class);
        dynamicCommentAddBo.setCurrentUserUuid(currentLoginUuid());
        dynamicCommentAddBo.setDynamicCommentUuid(dynamicCommentUuid);
        dynamicCommentService.reply(dynamicCommentAddBo);
        return success();
    }

    @GetMapping("/page/children-comment/{dynamicCommentUuid}")
    @ApiOperation("分页查询该评论下子集评论")
    public ApiResponse<IPage<DynamicCommentPageVo>> pageChildrenComment(
        @PathVariable(value = "dynamicCommentUuid", required = true) @ApiParam(value = "动态评论顶级uuid", required = true) String dynamicCommentUuid,
        @ApiParam(value = "表白墙分页查询Ro", required = false) DynamicCommentPageSearchRo dynamicCommentPageSearchRo) {
        DynamicCommentPageSearchBo dynamicCommentPageSearchBo = BeanUtil.copyProperties(
            dynamicCommentPageSearchRo, DynamicCommentPageSearchBo.class);
        dynamicCommentPageSearchBo.setDynamicCommentUuid(dynamicCommentUuid);
        return success(dynamicCommentService.pageChildrenComment(dynamicCommentPageSearchBo));
    }
}
